package org.example.biz.config;

import com.mongodb.ConnectionString;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;

/**
 * @Title : MongoConfig
 * @Description:
 * @author: libo@juzishuke.com
 * @date: 2023/12/14 10:36
 * @Version:1.0
 */
@Configuration
public class MongoConfig {


    @Value("${spring.data.mongodb.uri}")
    private String url;

    @Primary
    @Bean("deductMongoTemplate")
    public MongoTemplate deductMongoTemplate() throws Exception {
        return new MongoTemplate(deductFactory());
    }
    @Bean(name = "deductFactory")
    public SimpleMongoClientDatabaseFactory deductFactory() throws Exception {
        ConnectionString connectionString = new ConnectionString(url);
        return new SimpleMongoClientDatabaseFactory(connectionString);

    }
}
